package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * IoT设备管理Controller
 * 
 * @author laoYe
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "IoT设备管理相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询IoT设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取IoT设备管理列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出IoT设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "IoT设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出IoT设备管理列表")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "IoT设备管理数据");
    }

    /**
     * 获取IoT设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping(value="/{iotId}")
    @ApiOperation("获取设备详细信息")
    public R<DeviceDetailVo> getInfo(@ApiParam(value = "IoT设备管理ID", required = true) @PathVariable("iotId") String iotId) {
        DeviceDetailVo vo = deviceService.queryDeviceDetail(iotId);
        return R.ok(vo);
    }

    /**
     * 新增IoT设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "IoT设备管理", businessType = BusinessType.INSERT)
    @PostMapping("register")
    @ApiOperation("新增IoT设备")
    public R add(@ApiParam(value = "IoT设备管理实体", required = true) @RequestBody DeviceDto dto) {
        deviceService.insertDevice(dto);
        return R.ok();
    }

    /**
     * 修改IoT设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "IoT设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改IoT设备管理")
    public AjaxResult edit(@ApiParam(value = "IoT设备管理实体", required = true) @RequestBody Device device) {
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除IoT设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "IoT设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除IoT设备管理")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }

    /**
     * 产品同步
     */
    @PostMapping("syncProductList")
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "IoT设备管理", businessType = BusinessType.INSERT)
    @ApiOperation("同步IoT平台产品列表")
    public R syncProductList() {
        deviceService.syncProductList();
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping(value = "/allProduct")
    @ApiOperation("查询所有产品信息")
    public R<List<ProductVo>> getAllProducts() {
        return R.ok(deviceService.getAllProducts());
    }
    /**
     * 查询设备上报的最新数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报的最新数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable("iotId") String iotId) {
        List<DeviceShadowDataVo> voList = deviceService.queryServiceProperties(iotId);
        return R.ok(voList);
    }
}
